package com.atguigu.gulimall.gulimallsearch.testThread;

import java.util.concurrent.*;
import java.util.function.BiConsumer;

public class Test {
    //创建线程池，下面四种
    //固定大小core=max，不可回收
    public static Executor executor= Executors.newFixedThreadPool(10);
    //core=0，所有都是可回收的
    Executor executor1=Executors.newCachedThreadPool();
    //单线程线程池
    Executor executor2= Executors.newSingleThreadExecutor();
    ThreadPoolExecutor executor3=new ThreadPoolExecutor(10,
            20,
            10,
            TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(100),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());


    public static void main(String[] args) {
        System.out.println("main------start---");
//        CompletableFuture<Void> runAsync = CompletableFuture.runAsync(new runable01(), executor);

//        CompletableFuture<Void> runAsync = CompletableFuture.runAsync(()->{
//            System.out.println("当前线程"+Thread.currentThread().getId());
//            int i=10/2;
//            System.out.println("运行结果"+i);
//        }, executor).whenComplete((v,e)->{
//             System.out.println("结果："+v);
//             System.out.println("异常："+e);
//        });
        CompletableFuture<Integer> supplyAsync = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果" + i);
            return i;
        }, executor).whenComplete((t,e)->{
            System.out.println("结果：" + t);
            System.out.println("异常：" + e);
        });

        System.out.println("main------end-----");
    }

    public static class runable01 implements Runnable{

        @Override
        public void run() {
            System.out.println("当前线程"+Thread.currentThread().getId());
            int i=10/2;
            System.out.println("运行结果"+i);
        }
    }

}
